package org.okkam.refine.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;


import org.okkam.refine.ens.EnsEntity;
import org.okkam.refine.ens.EnsEntityFactory;
import org.okkam.refine.ens.EntityTypeSelector;
import org.okkam.refine.ens.client.OkkamClient;
import org.okkam.refine.ens.client.ServiceClient;
import org.openrdf.model.Resource;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.RepositoryException;

public class ApplicationController {

        Properties prop = new Properties();
        
        
       
	
	RepositoryConnection _model = null ;
		
	EnsEntityFactory factory = null;
	
	OkkamClient okkam = null;
	
	ServiceClient service = null ;
	
	
	EntityTypeSelector selector = null;
	
	public ApplicationController () {
	     //  URL url =  ClassLoader.getSystemResource("credentials.conf");
	       try{
                   //prop.load(new FileInputStream(new File(url.getFile()))); 
                   InputStream in =  this.getClass().getResourceAsStream("/conf/credentials/credentials.conf");
                   prop.load(in);
               }
               catch (Exception e) {
                System.out.println("Configuration file not found");
            }
	       String confPathFromProperty=prop.getProperty("confPath");
	       File file = new File(this.getClass().getResource(confPathFromProperty).getPath()); 
	       String confpath = file.getAbsolutePath();
	       okkam = new OkkamClient( confpath ) ;
	       service = new ServiceClient( okkam ) ;	                
	}
	
	public ApplicationController(RepositoryConnection model) {
	    
	  
		this._model=model;
		//URL url =  ClassLoader.getSystemResource("credentials.conf");
	       try{
	           //prop.load(new FileInputStream(new File(url.getFile()))); 
	           InputStream in =  this.getClass().getResourceAsStream("/conf/credentials/credentials.conf");
	           prop.load(in);
	       }
	       catch (Exception e) {
                System.out.println("Configuration file not found");
            }
	        String confPathFromProperty=prop.getProperty("confPath");
	        File file = new File(this.getClass().getResource(confPathFromProperty).getPath()); 
	        String confpath = file.getAbsolutePath();;
		okkam = new OkkamClient( confpath ) ;
		service = new ServiceClient( okkam ) ;
		selector = new EntityTypeSelector(_model) ;
	}
	
	
	public String createEntity(Resource node) throws RepositoryException {
		
		String result = "" ; // okkamid
		
		factory = selector.selectEntityFactory(node);
		
		EnsEntity entity = factory.createEnsEntity(_model, node);
				
		boolean ignoreDuplicates = false ; //no duplication or allow
		
		
		result = service.createNewEntity(entity.getSemanticType(), 
				entity.getAttributesType(), ignoreDuplicates) ;
		
		return result ;
	}
	
	public void updateEntity(Resource node, String okid) throws RepositoryException {
            
            factory = selector.selectEntityFactory(node);
            
            EnsEntity entity = factory.createEnsEntity(_model, node);
                            
            boolean ignoreDuplicates = true ; //no duplication or allow
            
            
            service.updateEntity(entity.getSemanticType(), entity.getAttributesType(), ignoreDuplicates, okid) ;
            
    }
	/**
	 * a boolean value to check success or failure of entity deletion
	 * @param okkamId to be deleted
	 * @return returns true if is successful else false
	 */
        
    public boolean deleteEntity(String okkamId) {
        boolean deleted = false ;
        String ticket = null ;
        try {

            //ticket = okkam.lockEntity(okkamId);

           deleted = service.deleteEntity(okkamId);

        } catch (Throwable t) {

            System.out.println(ticket + " not locked");

        }
        return deleted;

    }
    

}
